package managedbean;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import model.Book;
import model.Order;
import dao.OrderDao;


@ManagedBean(name = "orderManagedBean")
@SessionScoped
public class OrderManagedBean {

	private HashMap<Book,Integer> panier;
	private Order commande;
	@ManagedProperty(value = "#{orderDao}")
	private OrderDao orderDao;
	@ManagedProperty(value="#{connexionManagedBean}")
	private ConnexionManagedBean connexionManagedBean;

	public ConnexionManagedBean getConnexionManagedBean() {
		return connexionManagedBean;
	}

	public void setConnexionManagedBean(ConnexionManagedBean connexionManagedBean) {
		this.connexionManagedBean = connexionManagedBean;
	}

	public HashMap<Book,Integer> getPanier() {
		return panier;
	}

	public void setPanier(HashMap<Book,Integer> panier) {
		this.panier = panier;
	}

	public Order getCommande() {
		return commande;
	}

	public void setCommande(Order commande) {
		this.commande = commande;
	}
	
	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public OrderManagedBean (){
		panier=new HashMap<Book, Integer>();
	}
	
	public List<Book> getPanierKeys() {
		return new ArrayList<Book>(getPanier().keySet());
	}
	
	public String addBook (Book aBook){
		if(panier.containsKey(aBook)){
			panier.put(aBook, panier.get(aBook)+1);
		}else{
			panier.put(aBook, 1);
		}
		return "Added";
	}
	
	public void moreQuantityOfBook(Book book) {
		getPanier().put(book, getPanier().get(book)+1);
	}
	
	public void lessQuantityOfBook(Book book) {
		if (getPanier().get(book) == 1) {
			getPanier().remove(book);
		} else {
			getPanier().put(book, getPanier().get(book)-1);
		}
	}
	
	public void removeBook(Book book) {
		getPanier().remove(book);
	}
	
	public double totalOrder(){
		double sum = 0;
		ArrayList<Book> iteration=new ArrayList<Book>(getPanier().keySet());
		for (Book lBook:iteration){
			sum+=lBook.getPrice().doubleValue()*panier.get(lBook);
		}
		return sum;
	}
	
	public int nbElements(){
		int sum=0;
		ArrayList<Book> iteration=new ArrayList<Book>(getPanier().keySet());
		for (Book lBook:iteration){
			sum+=panier.get(lBook);
		}
		return sum;
	}
	
	public String save(){
		
		ArrayList<Book> iteration=new ArrayList<Book>(getPanier().keySet());
		if(commande==null){
			commande=new Order();
			commande.setClient(getConnexionManagedBean().getUser());
		}
		commande.setTotal(new BigDecimal(this.totalOrder()));
		commande.setDate(new Date());
		orderDao.setOrder(commande);
		
		for (Book lBook:iteration){
			orderDao.addItem(lBook,panier.get(lBook));
		}
		orderDao.save();
		this.commande=null;
		this.panier=new HashMap<Book, Integer>();
		return "Saved";
	}
	

}
